<?php
class FetchAction extends BaseAction{

    // 资源列表
     function fetchall(){
        $model_fetch = D('Fetch');
         $array_url = array();
        $fid = intval($_GET['fid']);

        $array_url['reurl'] =  htmlspecialchars(isset($_GET['reurl'])?$_GET['action']:"");
        $array_url['action'] = htmlspecialchars(isset($_GET['action'])?$_GET['action']:"");


        $data = M('Fetchurl')->where(array('fid'=>$fid))->find();
        $type = $data['type'];
        $url = $data['url'];
        $array_url['fid'] = $data['ffid'];
         $pages = $model_fetch->remote($data['url'],null,false);

        $toatalpages = 0;
        if(isset($pages[0]) && $pages[0])
        {
            if($type == 'json')
            {
                $json = json_decode($pages[0],true);
                $toatalpages =  $data['totalpage'] = $json['page']['pagecount'];
                M('Fetchurl')->data($data)->save();
                $fetchtimes = ceil($toatalpages/10);

            }else if($type == 'xml'){

                //组合分页信息
                preg_match('<list page="([0-9]+)" pagecount="([0-9]+)" pagesize="([0-9]+)" recordcount="([0-9]+)">',$pages[0],$page_array);
                $toatalpages = $data['totalpage'] = $page_array[2];
                M('Fetchurl')->data($data)->save();
                $fetchtimes = ceil($toatalpages/10);
            }


            $i = 0;
            do{
                $urls = array();
                for($ii=$i;$ii<$toatalpages,$ii<$i+10;$ii++)
                {

                    $urls[] = $url.$ii;
                }

              $pages2 = $model_fetch->remote($urls,null,false);
               foreach($pages2 as $page)
               {
                   if($type == 'json')
                   {
                       $model_fetch->json2data($page,$array_url);
                   }else if($type == 'xml')
                   {
                       $model_fetch->xml2data($page,$array_url);
                   }
               }

                $i +=10;
            }while($i<=$toatalpages);
        }



    }

    //下载远程图片
    public function down(){
        C('upload_http',1);
        $img = D('Img');
        $rs = D("Vod");

        $count = $rs->where('Left(vod_pic,7)="http://"')->count('vod_id');
        $i = 0;
        do{

            $list = $rs->where('Left(vod_pic,7)="http://"')->order('vod_addtime desc')->limit(100)->select();
            foreach($list as $key=>$value){
                $i++;
                $imgnew = $img->down_load($value['vod_pic'],'vod');
                if($value['vod_pic'] == $imgnew){
                    $rs->where('vod_id='.$value['vod_id'])->setField('vod_pic',str_replace("http://","httpf://",$value['vod_pic']));
                    echo $i." ".$value['vod_pic'].'下载失败!'."\n";
                }else{
                    $rs->where('vod_id = '.$value['vod_id'])->setField('vod_pic',$imgnew);
                    echo $i." ".$value['vod_pic'].'下载成功!'."\n";
                }
                ob_flush();flush();
            }
        }while($i<$count);


    }
}